<?php
/**
 * Generates and executes a users query
 *
 * @author Jason Raede <jason@torchdm.com>
 * @package Concerto
 * @subpackage Initializers
 */
 
namespace application\initializers;
class UsersQuery extends ConcertoQuery {
	
	/**
	 * Initializes the main query; takes parameters from the URL
	 */
	public function initMainQuery() {
		global $concerto;
		$queryVars = $concerto->queryVars;
		$query = self::buildQuery($queryVars);
		$results = DB()->getResults($query, 'object', 'application\\models\\User');
		$this->queries[] = array('results'=>$results, 'current'=>'');
		$this->queryPointer++;
			
		
	}
	
	/** 
	 * Initializes a query with the given parameters
	 *
	 * @param string $params A query string of parameters
	 * @see buildQuery();
	 */
	public function initQuery($params) {
		$this->params = $params;
		$params = self::parseParameters($params);
		$query = self::buildQuery($params);
		$results = DB()->getResults($query, 'object', 'application\\models\\User');
		$this->queries[] = array('results'=>$results, 'current'=>'');
		$this->queryPointer++;
	}
	
	/**
	 * Generates the opening SELECT statement of the SQL query based on the parameters given
	 *
	 * @param array $params The parameters provided when initializing the query
	 * @return string The generated statement
	 */
	protected static function selectStatement($params) {
		$sql = "SELECT * FROM `cn_users` LEFT JOIN `cn_user_content_relations` ON `user_id`=`ucr_user`";
		
		return Modulator()->runFilter('users_query_select_statement', $sql, $params);
	}
	
	/**
	 * Builds the query based on the given parameters.
	 *
	 * @param array $params The query parameters
	 * @return string The generated SQL query
	 */
	public function buildQuery($params) {
		global $concerto;
		$queries = array();
		
		// User ID
		if($id = $params['id']) {
			if(is_array($id)) $id = "'".implode("', '", $id)."'";
			else $id = "'".$id."'";
			$queries['id'] = "`user_id` IN ($id)";
		}
		
		// NOT Content ID
		if($notId = $params['notId']) {
			if(is_array($notId)) $notId = "'".implode("', '", $notId)."'";
			else $notId = "'".$notId."'";
			$queries['notId'] = "`user_id` NOT IN ($notId)";
		}
		
		// Level
		if($level = $params['level']) {
			switch($level) {
				case 'global_admin':
					$queries['level'] = "`user_level`='2'";
					break;
				case 'local_admin':
					$queries['level'] = "`user_level`='1'";
					break;
				case 'normal':
					$queries['level'] = "`user_level`='0'";
					break;
				case 'pending':
					$queries['level'] = "`user_level`='-1'";
					break;
			}
		}
		
		
		// First Letter (for alphabetical display)
		if($firstLetter = $params['firstLetter']) {
			if(ctype_alpha($firstLetter)) {
				$queries['firstLetter'] = "`user_username` LIKE '{$firstLetter}%'";
			}
			else { // Numbers & symbols
				$queries['firstLetter'] = "LEFT(`user_username`, 1) NOT REGEXP '[A-Za-z]'";
			}
		}
		
		// Has interaction with content
		if(($interaction = $params['interaction']) && ($interactionContent = $params['interactionContent'])) {
			$queries['interaction'] = "`ucr_interaction`='$interaction' AND `ucr_content`='$interactionContent'";
		
		}
		
		
		// Search
		if($search = $params['search']) {
			$queries['search'] = "`user_username` LIKE '%$search%' OR `user_email` LIKE '%$search%'";
		}
		

		$queries = Modulator()->runFilter('user_queries', $queries, $params);
		
		$generalOperator = ($params['generalOperator'] == 'OR' || $params['generalOperator'] == 'AND') ? $params['generalOperator'] : 'AND';
		if(count($queries))
			$sql = ' WHERE '.implode(' '.$generalOperator.' ', $queries);
		else 
			$sql = ' ';
		
		
		$sql = self::selectStatement($params) .$sql;
		
		// Now paginate
		$page = ($params['page']) ? $params['page'] : 1;
		$limit = ($params['limit']) ? $params['limit'] : 20;
		$limitClause = " LIMIT " . ($limit*($page - 1)).", ".$limit;
		
		return $sql.$limitClause;
		
		
	}
	
	
	
	
}

?>